'''
Copyright 2010 Indra Bagus <indra.bagus@gmail.com> 
    
Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

    http://www.apache.org/licenses/LICENSE-2.0 
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. 
'''



import unittest
import Decoder
from Decoder import MACHeaderGenericDecoder, TLVDecoder, ULMAPDecoder,\
    UCDDecoder, PDUDecoder, PDUMMError
from test import test_support
import Tools
from WimaxStreamHandler import ULMAPHandler, UCDHandler,\
    MMHandlerCollection, DLMAPHandler, DCDHandler
from ManagementMessage import ULMap
from ULMapIE import ULMapIEBurstProfile




class MACHeaderGenericDecoderTester(unittest.TestCase):
    def testHCS(self):
        test_vector = '\x00\x00\xD4\x12\x34\x75'
        mheader_generic = MACHeaderGenericDecoder(test_vector)
        self.assertEqual(mheader_generic.calculated_hcs ,0x75   )
        
    
    def testMACHeaderGeneric(self):
        test_vector = '\x00\x00\xD4\x12\x34\x75'
        macheader  = MACHeaderGenericDecoder(test_vector).Decode()
        self.assertEqual(macheader.header_type.val,0,'MAC Header type')
        self.assertEqual(str(macheader.header_type),'Generic','MAC Header type string')
        self.assertEqual(macheader.encryption.val,0,'MAC Header EC')
        self.assertEqual(str(macheader.encryption),'Disable','MAC Header EC string')
        self.assertEqual(macheader.encoding_type.arq_feedback.val,0,'Encoding type for ARQ feedback')
        self.assertEqual(str(macheader.encoding_type.arq_feedback),'Absent')
        self.assertEqual(macheader.encoding_type.extended_type.val,0)
        self.assertEqual(str(macheader.encoding_type.extended_type),'Not Extended')
        self.assertEqual(macheader.encoding_type.frag_subheader.val,0)
        self.assertEqual(str(macheader.encoding_type.frag_subheader),'Absent')
        self.assertEqual(macheader.encoding_type.packing_subheader.val,0)
        self.assertEqual(str(macheader.encoding_type.packing_subheader),'Absent')
        self.assertEqual(macheader.esf.val, 0)
        self.assertEqual(str(macheader.esf),'Absent')



class TLVDecoderTester(unittest.TestCase):
    '''
    Test case untuk semua skenario TLV decoder
    '''
    def testTLVDecoder(self):
        test_vector = '\x01\x02\xAA\xBB\x02\x04\x0A\x0B\x0C\x0D'
        decoder = TLVDecoder(test_vector)
        tlv_list = decoder.Decode()
        self.assertEqual(len(tlv_list),2)
        self.assertEqual(int(tlv_list[0].Type),0x01)
        self.assertEqual(tlv_list[0].Length,0x02)
        self.assertEqual(tlv_list[0].Value.ToInteger(),0xAABB)
        self.assertEqual(int(tlv_list[1].Type),0x02)
        self.assertEqual(tlv_list[1].Length,4)
        self.assertEqual(tlv_list[1].Value.ToInteger(),0x0A0B0C0D)

        
        
    def testLongTLVDecoder(self):
        tlv_1       =   '\x02\x82\x01\x1D'
        tlv_1_data  =   'The CDT master update archive is available '\
                        'for each release as well. Download the zip '\
                        'file and enter the path to it in the Software '\
                        'Update as an Achived update site to install CDT. '\
                        'Please note that unzipping the master zip file into '\
                        'the eclipse directory will not install CDT properly.'
        
        tlv_2       =   '\x03\x82\x01\x15'
        tlv_2_data  =   'Convert a value to a Boolean, using the standard truth testing '\
                        'procedure. If x is false or omitted, this returns False; otherwise '\
                        'it returns True. bool is also a class, which is a subclass of int. '\
                        'Class bool cannot be subclassed further. Its only instances are '\
                        'False  and True.'
                        
        test_vector = tlv_1 + tlv_1_data + tlv_2 + tlv_2_data
                        
        decoder = TLVDecoder(test_vector)
        tlvlist = decoder.Decode()
        self.assertEqual(len(tlvlist),2)
        self.assertEqual(int(tlvlist[0].Type),2)
        self.assertEqual(tlvlist[0].Length,285)
        self.assertEqual(tlvlist[0].Value.GetValueAsString(),tlv_1_data)
        self.assertEqual(int(tlvlist[1].Type),3)
        self.assertEqual(tlvlist[1].Length,277)
        self.assertEqual(tlvlist[1].Value.GetValueAsString(),tlv_2_data)        
        
            

class ToolsTester(unittest.TestCase):
    '''
    Unit test untuk modul tools
    '''
    
    def testDumpStringBuf(self):
        input = '\xAA\xBC\01\x02'
        output = '| AA BC 01 02 |'
        result = Tools.DumpBuffString(input,4)
        self.assertEqual(result,output)
        
        
    def testDumpListBuf(self):
        input = [0x01,0x02,0x03,0x04,0x05]
        output = '| 01 02 03 04 05 |'
        result = Tools.DumpBuffList(input,5)
        self.assertEqual(result,output)
        
        
    def testIntegerToListInteger(self):
        input = 0x010203040506
        output_byte = [0x01,0x02,0x03,0x04,0x05,0x06]
        result = Tools.IntegerToListInteger(input, 6, 8)
        self.assertEqual(result,output_byte)
        output_short = [0x0102,0x0304,0x0506]
        result = Tools.IntegerToListInteger(input, 3, 16)
        self.assertEqual(result,output_short)
    
    
    def testCRCOFDMA(self):
        test_vector =   '\x40\x40\x1A\x06\xC4\x5A\xBC\xF6\x57\x21'\
                        '\xE7\x55\x36\xC8\x27\xA8\xD7\x1B\x43\x2C\xA5\x48'
        crc32 = Tools.CalculateCRC32OFDMA(test_vector)
        self.assertEqual(crc32,0x1BD1BA21)
        
        test_vector =   '\x40\x40\x37\x7E\xB2\xC7\x08\x7D\xD0\x78\x71\x3F\xB1\x22\xB9\x73'\
                        '\x4F\xDB\xFD\x68\x2E\xAD\x9D\xCA\x9F\x44\x1F\x62\xFE\x0F\x4A\x2C'\
                        '\x45\xB5\x53\x17\x3D\x66\x5B\x2D\x53\xC1\xB3\xE7\xE4\x8D\x2D\xB7'\
                        '\x61\xCF\x94'
        crc32 = Tools.CalculateCRC32OFDMA(test_vector)
        self.assertEqual(crc32,0xFD037B1D)
        
        
        
    def testCRCOFDMAReceiver(self):
        test_vector =   '\x40\x40\x1A\x06\xC4\x5A\xBC\xF6\x57\x21'\
                        '\xE7\x55\x36\xC8\x27\xA8\xD7\x1B\x43\x2C\xA5\x48\x1B\xD1\xBA\x21'
                        
                        
        crc32receiver = Tools.CalculateCRC32OFDMAReceiver(test_vector)
        self.assertEqual(crc32receiver,0xC704DD7B)
        test_vector =   '\x40\x40\x37\x7E\xB2\xC7\x08\x7D\xD0\x78\x71\x3F\xB1\x22\xB9\x73'\
                        '\x4F\xDB\xFD\x68\x2E\xAD\x9D\xCA\x9F\x44\x1F\x62\xFE\x0F\x4A\x2C'\
                        '\x45\xB5\x53\x17\x3D\x66\x5B\x2D\x53\xC1\xB3\xE7\xE4\x8D\x2D\xB7'\
                        '\x61\xCF\x94\xFD\x03\x7B\x1D'
        crc32receiver = Tools.CalculateCRC32OFDMAReceiver(test_vector)
        self.assertEqual(crc32receiver,0xC704DD7B)



class ULMapDecoderTester(unittest.TestCase):
    '''
    Test case untuk proses decoding ULMAP
    '''
    
    
    class ULMapDecoderHandler(ULMAPHandler):
        '''
        Nested Class yang digunakan untuk menghandle ULMAP Decoder
        '''
        def __init__(self):
            ULMAPHandler.__init__(self)
            self.ulmap      = None
            self.macheader  = None
            self.ulmapie_burst_profile_list = []
            self.ulmapie_power_ctrl = None
            self.ulmapie_aas_ul = None
            self.ulmapie_ulzone = None
            
        def OnStartPDU(self,payload,macheader):
            super(ULMapDecoderTester.ULMapDecoderHandler,self).OnStartPDU(payload,macheader)
            self.macheader = macheader
            
        def OnReceiveULMAP(self,ulmap):
            super(ULMapDecoderTester.ULMapDecoderHandler,self).OnReceiveULMAP(ulmap)
            self.ulmap = ulmap
            
        def OnReceiveULMAPIeBurstProfile(self,ulmapie):
            super(ULMapDecoderTester.ULMapDecoderHandler,self).OnReceiveULMAPIeBurstProfile(ulmapie)
            self.ulmapie_burst_profile_list.append(ulmapie)
            
        def OnReceivePowerControl(self,ulmapie):
            super(ULMapDecoderTester.ULMapDecoderHandler,self).OnReceivePowerControl(ulmapie)
            self.ulmapie_power_ctrl = ulmapie
            
        def OnReceiveAAS_UL(self,ulmapie):
            super(ULMapDecoderTester.ULMapDecoderHandler,self).OnReceivePowerControl(ulmapie)
            self.ulmapie_aas_ul = ulmapie
            
        def OnReceiveULZone(self,ulmapie):
            super(ULMapDecoderTester.ULMapDecoderHandler,self).OnReceiveULZone(ulmapie)
            self.ulmapie_ulzone = ulmapie                    
    
                
    def testULMapIeBurstProfiles(self):
            test_vector =   '\x00\x00\x1E\xFF\xFF\xAA\x03\x80\x0C\xAB\xCD\xEF\x12\xFF\x00\x05'\
                            '\x10\x04\x00\x02\x20\x0D\x00\x03\x30\x16\x00\x07\x40\x0B'
                            
            handler = ULMapDecoderTester.ULMapDecoderHandler()
            mmhandlercollection = MMHandlerCollection()
            mmhandlercollection.ulmaphandler = handler
            PDUDecoder(mmhandlercollection).Decode(test_vector)
            self.assertTrue(handler.macheader != None)
            self.assertTrue(handler.ulmap!= None)
            self.assertEqual(handler.ulmap.fdd_part_flag,1)
            self.assertEqual(handler.ulmap.ucd_count,12)
            self.assertEqual(handler.ulmap.alloc_start_time,0xABCDEF12)            
            self.assertEqual(handler.ulmap.no_ofdma_symbols,0xFF)
            
            #checking the burst profile list
            self.assertEqual(len(handler.ulmapie_burst_profile_list),4)     
            #checking 1st burst profile list
            self.assertEqual(handler.ulmapie_burst_profile_list[0].cid,5)
            self.assertEqual(int(handler.ulmapie_burst_profile_list[0].uiuc),1)            
            self.assertEqual(handler.ulmapie_burst_profile_list[0].duration,1) 
            self.assertEqual(handler.ulmapie_burst_profile_list[0].repet_coding_ind.val,0)                           
            self.assertEqual(str(handler.ulmapie_burst_profile_list[0].repet_coding_ind),'No repetition coding')                                       
            #checking 2nd burst profile list
            self.assertEqual(handler.ulmapie_burst_profile_list[1].cid,2)
            self.assertEqual(int(handler.ulmapie_burst_profile_list[1].uiuc),2)            
            self.assertEqual(handler.ulmapie_burst_profile_list[1].duration,3) 
            self.assertEqual(handler.ulmapie_burst_profile_list[1].repet_coding_ind.val,1)
            self.assertEqual(str(handler.ulmapie_burst_profile_list[1].repet_coding_ind),'Repetition coding of 2')            
            #checking 3rd burst profile list
            self.assertEqual(handler.ulmapie_burst_profile_list[2].cid,3)
            self.assertEqual(int(handler.ulmapie_burst_profile_list[2].uiuc),3)            
            self.assertEqual(handler.ulmapie_burst_profile_list[2].duration,5)
            self.assertEqual(handler.ulmapie_burst_profile_list[2].repet_coding_ind.val,2)
            self.assertEqual(str(handler.ulmapie_burst_profile_list[2].repet_coding_ind),'Repetition coding of 4')            
            
            #checking 3rd burst profile list
            self.assertEqual(handler.ulmapie_burst_profile_list[3].cid,7)
            self.assertEqual(int(handler.ulmapie_burst_profile_list[3].uiuc),4)            
            self.assertEqual(handler.ulmapie_burst_profile_list[3].duration,2)
            self.assertEqual(handler.ulmapie_burst_profile_list[3].repet_coding_ind.val,3)
            self.assertEqual(str(handler.ulmapie_burst_profile_list[3].repet_coding_ind),'Repetition coding of 6')                        
    
    
    def testULMapIeCorruptBurstProfiles(self):
        test_vector =   '\x00\x00\x1E\xFF\xFF\xAA\x03\x80\x0C\xAB\xCD\xEF\x12\xFF\x00\x05'\
                        '\x10\x04\x00\x02\x20\x0D\x00'
                            
        handler = ULMapDecoderTester.ULMapDecoderHandler()
        mmhandlercollection = ManagementMessageHandler()
        mmhandlercollection.ulmaphandler = handler
        iserr = False
        try:
            PDUDecoder(mmhandlercollection).Decode(test_vector)
        except PDUMMError:
            iserr = True
        self.assertTrue(iserr)    
    
    
    def testULMapIeRangingOpportunity(self):
        pass
    
    
    def testULMapIeCDMAAlloc(self):
        pass

    
    def testULMapIeRngBurstProfiles(self):
        pass
        
        

class UCDDecoderTester(unittest.TestCase):
    '''
    Test case object yang berfungsi sebagai UCD Decoder unit tester
    '''
    
    class UCDDecoderHandler(UCDHandler):
        def __init__(self):
            self.ucdparam = None
            self.macheader = None
            self.tlv_list = list()
            self.uplink_burst_profile = list()
        

        def OnStartPDU(self,payload,macheader):
            super(UCDDecoderTester.UCDDecoderHandler,self).OnStartPDU(payload,macheader)
            self.macheader = macheader

        
        def OnReceiveUCD(self,ucd_header):
            super(UCDDecoderTester.UCDDecoderHandler,self).OnReceiveUCD(ucd_header)
            self.ucdparam = ucd_header
    
        def OnReceiveULBurstProfile(self,uplinkburst):
            super(UCDDecoderTester.UCDDecoderHandler,self).OnReceiveULBurstProfile(uplinkburst)
            self.uplink_burst_profile.append(uplinkburst)
            
        def OnReceiveULChannelEncoding(self,tlv):
            super(UCDDecoderTester.UCDDecoderHandler,self).OnReceiveULChannelEncoding(tlv)
            self.tlv_list.append(tlv)
            
            
    def testUCDNoUplinkBurstProfile(self):
        test_vector =   '\x00\x00\x0F\xFF\xFF\x63\x00\x02\x03\x04\x05\x06\x05\x04\x00\x00'\
                        '\x32\x00\x96\x01\x01'
        handler = UCDDecoderTester.UCDDecoderHandler()
        mmhandler = ManagementMessageHandler()
        mmhandler.ucdhandler = handler
        PDUDecoder(mmhandler).Decode(test_vector)
        self.assertTrue(handler.ucdparam != None)
        self.assertTrue(handler.macheader != None)
        self.assertTrue(len(handler.tlv_list) == 2)       
        self.assertTrue(len(handler.uplink_burst_profile) == 0)
        
        #Verify the macheader
        self.assertEqual(str(handler.macheader.header_type),'Generic')
        self.assertEqual(str(handler.macheader.encryption),'Disable')
        
        #Verify all ucd param
        self.assertEqual(handler.ucdparam.configuration_change_count,2)
        self.assertEqual(handler.ucdparam.ranging_back_off_start,3)
        self.assertEqual(handler.ucdparam.ranging_back_off_end, 4)
        self.assertEqual(handler.ucdparam.request_back_off_start,5)
        self.assertEqual(handler.ucdparam.request_back_off_end,6)      

        validtlv = [(5,4,0x3200),(150,1,0x01)]
        itervalid = iter(validtlv)
        itertester = iter(handler.tlv_list)
        while True:
            try:
                validtlv = itervalid.next()
                testedtlv = itertester.next()
                self.assertEqual(int(testedtlv.Type),validtlv[0])
                self.assertEqual(testedtlv.Length,validtlv[1])
                self.assertEqual(testedtlv.Value.ToInteger(),validtlv[2])
            except StopIteration:
                break

    
    def testUCDWithUplinkBurstProfile(self):
        pass
    
    
    def testUCDWithUplinkBurstProfiles(self):
        
        test_vector =   '\x00\x00\x33\xFF\xFF\x78\x00\x02\x03\x04\x05\x06\x05\x04\x00\x00'\
                        '\x32\x00\x96\x01\x01\x01\x0A\x05\x96\x01\x00\x97\x01\x01\x98\x01'\
                        '\x02\x01\x0A\x03\x96\x01\x02\x97\x01\x02\x98\x01\x03\x01\x0A\x02'\
                        '\x96\x01\x04\x97\x01\x05\x98\x01\x06'

                        
        handler = UCDDecoderTester.UCDDecoderHandler()
        macheader = MACHeaderGenericDecoder(test_vector[:6]).Decode()
        UCDDecoder(handler).Decode(macheader, test_vector)
        self.assertTrue(handler.ucdparam != None)
        self.assertTrue(handler.macheader != None)
        self.assertTrue(len(handler.tlv_list) == 2)        
        self.assertTrue(len(handler.uplink_burst_profile) == 3)                   

        #Verify all ucd param
        self.assertEqual(handler.ucdparam.configuration_change_count,2)
        self.assertEqual(handler.ucdparam.ranging_back_off_start,3)
        self.assertEqual(handler.ucdparam.ranging_back_off_end,4)
        self.assertEqual(handler.ucdparam.request_back_off_start,5)
        self.assertEqual(handler.ucdparam.request_back_off_end,6) 
        
        #Verify first Burst profile element
        self.assertEqual(handler.uplink_burst_profile[0].uiuc,5)        
        self.assertEqual(len(handler.uplink_burst_profile[0].tlv_list),3)
        self.assertEqual(int(handler.uplink_burst_profile[0].tlv_list[0].Type),150)
        self.assertEqual(handler.uplink_burst_profile[0].tlv_list[0].Length,1)
        self.assertEqual(handler.uplink_burst_profile[0].tlv_list[0].Value.ToInteger(),0)
        self.assertEqual(int(handler.uplink_burst_profile[0].tlv_list[1].Type),151)
        self.assertEqual(handler.uplink_burst_profile[0].tlv_list[1].Length,1)
        self.assertEqual(handler.uplink_burst_profile[0].tlv_list[1].Value.ToInteger(),1)                
        self.assertEqual(int(handler.uplink_burst_profile[0].tlv_list[2].Type),152)
        self.assertEqual(handler.uplink_burst_profile[0].tlv_list[2].Length,1)
        self.assertEqual(handler.uplink_burst_profile[0].tlv_list[2].Value.ToInteger(),2)
        
        #Verify second Burst profile element
        self.assertEqual(handler.uplink_burst_profile[1].uiuc,3)        
        self.assertEqual(len(handler.uplink_burst_profile[1].tlv_list),3)
        self.assertEqual(int(handler.uplink_burst_profile[1].tlv_list[0].Type),150)
        self.assertEqual(handler.uplink_burst_profile[1].tlv_list[0].Length,1)
        self.assertEqual(handler.uplink_burst_profile[1].tlv_list[0].Value.ToInteger(),2)
        self.assertEqual(int(handler.uplink_burst_profile[1].tlv_list[1].Type),151)
        self.assertEqual(handler.uplink_burst_profile[1].tlv_list[1].Length,1)
        self.assertEqual(handler.uplink_burst_profile[1].tlv_list[1].Value.ToInteger(),2)                
        self.assertEqual(int(handler.uplink_burst_profile[1].tlv_list[2].Type),152)
        self.assertEqual(handler.uplink_burst_profile[1].tlv_list[2].Length,1)
        self.assertEqual(handler.uplink_burst_profile[1].tlv_list[2].Value.ToInteger(),3)        
        
        #Verify third Burst profile element
        self.assertEqual(handler.uplink_burst_profile[2].uiuc,2)        
        self.assertEqual(len(handler.uplink_burst_profile[1].tlv_list),3)
        self.assertEqual(int(handler.uplink_burst_profile[2].tlv_list[0].Type),150)
        self.assertEqual(handler.uplink_burst_profile[2].tlv_list[0].Length,1)
        self.assertEqual(handler.uplink_burst_profile[2].tlv_list[0].Value.ToInteger(),4)
        self.assertEqual(int(handler.uplink_burst_profile[2].tlv_list[1].Type),151)
        self.assertEqual(handler.uplink_burst_profile[2].tlv_list[1].Length,1)
        self.assertEqual(handler.uplink_burst_profile[2].tlv_list[1].Value.ToInteger(),5)                
        self.assertEqual(int(handler.uplink_burst_profile[2].tlv_list[2].Type),152)
        self.assertEqual(handler.uplink_burst_profile[2].tlv_list[2].Length,1)
        self.assertEqual(handler.uplink_burst_profile[2].tlv_list[2].Value.ToInteger(),6)                
        
                       
        self.assertEqual(len(handler.uplink_burst_profile[1].tlv_list),3)
        
    
    def testUCDCompletePayload(self):
        pass
      
    
    
class DLMAPDecoderTester(unittest.TestCase):
    class DLMapDecoderHandler(DLMAPHandler):
        def __init__(self):
            super(DLMAPDecoderTester.DLMapDecoderHandler,self).__init__()
            self.burstprofile_list = []
            self.dlmap                      = None
            self.dlmapie_cid_switch         = None
            self.dlmapie_chan_measurement   = None
            self.dlmapie_stc_zone           = None
            self.dlmapie_aasdl              = None
            self.dlmapie_datalocaanotherbs  = None
            self.dlmapie_phymod             = None
            self.dlmapie_broadcast_ctrl_ptr = None

        
        def OnReceiveDLMAP(self,dlmap):
            super(DLMAPDecoderTester.DLMapDecoderHandler,self).OnReceiveDLMAP(dlmap)
            self.dlmap = dlmap

    
        def OnReceiveDLMAPIeBurstProfile(self,dlmapie):
            super(DLMAPDecoderTester.DLMapDecoderHandler,self).OnReceiveDLMAPIeBurstProfile(dlmapie)
            self.burstprofile_list.append(dlmapie)


    
        def OnReceiveDLMAPIeCIDSwitch(self,dlmapie):
            super(DLMAPDecoderTester.DLMapDecoderHandler,self).OnReceiveDLMAPIeCIDSwitch(dlmapie)
            self.dlmapie_cid_switch = dlmapie


            
        def OnReceiveDLMAPIEChannelMeasurement(self,dlmapie):
            super(DLMAPDecoderTester.DLMapDecoderHandler,self).OnReceiveDLMAPIEChannelMeasurement(dlmapie)
            self.dlmapie_chan_measurement = dlmapie



        def OnReceiveSTC_Zone(self,dlmapie):
            super(DLMAPDecoderTester.DLMapDecoderHandler,self).OnReceiveSTC_Zone(dlmapie)
            self.dlmapie_stc_zone = dlmapie

            
        def OnReceiveAASDL(self,dlmapie):
            super(DLMAPDecoderTester.DLMapDecoderHandler,self).OnReceiveAASDL(dlmapie)
            self.dlmapie_aasdl = dlmapie
            
            
        def OnReceiveDataLocationAnotherBS(self,dlmapie):
            super(DLMAPDecoderTester.DLMapDecoderHandler,self).OnReceiveDataLocationAnotherBS(dlmapie)
            self.dlmapie_datalocaanotherbs = dlmapie
            
        
        def OnReceivePhysicalModifier(self,dlmapie):
            super(DLMAPDecoderTester.DLMapDecoderHandler,self).OnReceivePhysicalModifier(dlmapie)
            self.dlmapie_phymod = dlmapie
            
            
        def OnReceiveBroadcastCtrlPointer(self,dlmapie):            
            super(DLMAPDecoderTester.DLMapDecoderHandler,self).OnReceiveBroadcastCtrlPointer(dlmapie)
            self.dlmapie_broadcast_ctrl_ptr = dlmapie
            
                        
                
    def testBurstProfile(self):
        test_vector =   '\x00\x00\x2E\xFF\xFF\x4B\x02\xAA\xAA\xCC\xDD\xA2\xAB\xCD\xEF\x02\xAA\xFF\x02\x06\x40\x38'\
                        '\x00\x01\x0A\x03\x00\x00\x21\x20\x18\x00\x03\x0D\x02\x00\x00\x50'\
                        '\xB0\x28\x00\x07\x0C\x00\x00\x00'
                        
        handlercollection = ManagementMessageHandler()
        handler = DLMAPDecoderTester.DLMapDecoderHandler()
        handlercollection.dlmaphandler  = handler
        PDUDecoder(handlercollection).Decode(test_vector)
        self.assertTrue(handler.dlmap != None)
        self.assertTrue(len(handler.burstprofile_list) == 6)


    def testDLMapIE_STCZone(self):
        test_vector =   '\x00\x00\x22\xFF\xFF\xB1\x02\xCD\xAD\xBC\xA0\xA2\xAB\xCD\xEF\x02'\
                        '\xBA\xDF\x02\x10\xA0\xB0\xC3\xE9\x0B\x0F\x8D\x41\xF1\x40\xA7\x68'\
                        '\xA1\x00'


        handlercollection = ManagementMessageHandler()
        handler = DLMAPDecoderTester.DLMapDecoderHandler()
        handlercollection.dlmaphandler  = handler
        PDUDecoder(handlercollection).Decode(test_vector)
        self.assertTrue(handler.dlmapie_stc_zone != None)
        self.assertEqual(int(handler.dlmapie_stc_zone.ofdma_symbol_offset),10)
        self.assertEqual(int(handler.dlmapie_stc_zone.permutation),1)
        self.assertEqual(int(handler.dlmapie_stc_zone.use_all_sc),1)
        self.assertEqual(int(handler.dlmapie_stc_zone.stc),2)
        self.assertEqual(int(handler.dlmapie_stc_zone.matrix_ind),3)
        self.assertEqual(int(handler.dlmapie_stc_zone.dl_permbase),8)
        self.assertEqual(int(handler.dlmapie_stc_zone.prbs_id),2)
        self.assertEqual(int(handler.dlmapie_stc_zone.amc_type),2)
        self.assertEqual(int(handler.dlmapie_stc_zone.midamble_presence),0)
        self.assertEqual(int(handler.dlmapie_stc_zone.midamble_boosting),0)
        self.assertEqual(int(handler.dlmapie_stc_zone.antennas_2_3_select),0)
        self.assertEqual(int(handler.dlmapie_stc_zone.dedicated_pilots),1)
                
        
    def testDLMapIECidSwitch(self):
        test_vector = '\x00\x00\x33\xFF\xFF\x78\x02\xCD\xAD\xBC\xA0\xA2\xAB\xCD\xEF\x02'\
                      '\xBA\xDF\x02\x10\xA0\xB0\xC3\xEF\x40\x20\xAA\x00\x1B\x00\x2C\x00'\
                      '\x3D\x00\x4E\x00\x5F\x00\x6A\x00\x7B\x00\x8C\x00\x9D\x00\xA0\xB0'\
                      '\xF8\xD3\x90'

        handlercollection = ManagementMessageHandler()
        handler = DLMAPDecoderTester.DLMapDecoderHandler()
        handlercollection.dlmaphandler  = handler
        PDUDecoder(handlercollection).Decode(test_vector)
        self.assertTrue(handler.dlmapie_cid_switch != None)
        self.assertEqual(len(handler.burstprofile_list),2)
        burstprofile = handler.burstprofile_list[0]
        self.assertEqual(len(burstprofile.cid_list),0)
        self.assertEqual(int(burstprofile.ofdma_symbol_offset),10)
        self.assertEqual(int(burstprofile.subchannel_offset),2)
        self.assertEqual(int(burstprofile.boosting),6)
        self.assertEqual(int(burstprofile.num_ofdma_symbols),12)
        self.assertEqual(int(burstprofile.num_subchannels),15)
        self.assertEqual(int(burstprofile.repet_coding_ind),2)
        
        # verified second burst profile
        burstprofile = handler.burstprofile_list[1]
        self.assertEqual(len(burstprofile.cid_list),10)
        self.assertEqual(burstprofile.cid_list[0],0xA001)


    def testDLMAPIE_AASDL(self):
        test_vector =   '\x00\x40\x25\xFF\xFF\x3C\x02\xED\xAC\xBC\xA0\xA2\xDE\xAD\xDE\xAD'\
                        '\x00\x00\x03\xF2\x3E\xF4\x6F\x81\x0C\x08\x0A\x32\x20\xD0\xC8\xB3'\
                        '\x70\x97\x6B\x9B\x23'
                        
        handlercollection = ManagementMessageHandler()
        handler = DLMAPDecoderTester.DLMapDecoderHandler()
        handlercollection.dlmaphandler  = handler
        PDUDecoder(handlercollection).Decode(test_vector)
        self.assertTrue(handler.dlmapie_aasdl != None)
        self.assertEqual(int(handler.dlmapie_aasdl.diuc),15)        
        self.assertEqual(int(handler.dlmapie_aasdl.ext_diuc),0x02)                
        self.assertEqual(int(handler.dlmapie_aasdl.ofdma_symbol_offset),0xEF)
        self.assertEqual(int(handler.dlmapie_aasdl.permutation),0x02)
        self.assertEqual(int(handler.dlmapie_aasdl.dl_permbase),0x0D)
        self.assertEqual(int(handler.dlmapie_aasdl.dl_preamble_config),0x03)
        self.assertEqual(int(handler.dlmapie_aasdl.preamble_type),0x01)
        self.assertEqual(int(handler.dlmapie_aasdl.prbs_id),0x02)
        self.assertEqual(int(handler.dlmapie_aasdl.diversity_map),0x00)
        self.assertEqual(len(handler.burstprofile_list),2)
        self.assertEqual(handler.burstprofile_list[0].diuc,0x01)   
        self.assertEqual(handler.burstprofile_list[0].ofdma_symbol_offset,12)           
        self.assertEqual(handler.burstprofile_list[0].subchannel_offset,2)
        self.assertEqual(int(handler.burstprofile_list[0].boosting),0)
        self.assertEqual(handler.burstprofile_list[0].num_ofdma_symbols,10)
        self.assertEqual(handler.burstprofile_list[0].num_subchannels,12)
        self.assertEqual(int(handler.burstprofile_list[0].repet_coding_ind),2)                                                                                               
 
        self.assertEqual(handler.burstprofile_list[1].diuc,0x02)   
        self.assertEqual(handler.burstprofile_list[1].ofdma_symbol_offset,13)           
        self.assertEqual(handler.burstprofile_list[1].subchannel_offset,3)
        self.assertEqual(int(handler.burstprofile_list[1].boosting),1)
        self.assertEqual(handler.burstprofile_list[1].num_ofdma_symbols,11)
        self.assertEqual(handler.burstprofile_list[1].num_subchannels,13)
        self.assertEqual(int(handler.burstprofile_list[1].repet_coding_ind),3)        


    def testDLMAPIE_DataLocAnotherBS(self):
        test_vector =   '\x00\x40\x2F\xFF\xFF\xBB\x02\xED\xAC\xBC\xA0\xA2\xDE\xAD\xDE\xAD'\
                        '\x00\x00\x03\x10\xC0\x80\xA3\x2F\x39\xE5\xAE\xAB\x9E\xCD\xFA\xB6'\
                        '\xAB\xAB\x20\xD0\xC8\xB3\x73\x0C\x08\x0A\x32\xA7\xBF\x15\x5D'
                        
        handlercollection = ManagementMessageHandler()
        handler = DLMAPDecoderTester.DLMapDecoderHandler()
        handlercollection.dlmaphandler  = handler
        PDUDecoder(handlercollection).Decode(test_vector)
        self.assertTrue(handler.dlmapie_datalocaanotherbs != None)
        self.assertEqual(int(handler.dlmapie_datalocaanotherbs.segment),3)
        self.assertEqual(int(handler.dlmapie_datalocaanotherbs.used_subchannels),0x25)
        self.assertEqual(int(handler.dlmapie_datalocaanotherbs.diuc),0x0A)
        self.assertEqual(int(handler.dlmapie_datalocaanotherbs.frame_advance),0x07)
        self.assertEqual(int(handler.dlmapie_datalocaanotherbs.ofdma_symbol_offset),0xAB)
        self.assertEqual(int(handler.dlmapie_datalocaanotherbs.subchannel_offset),0x27)
        self.assertEqual(int(handler.dlmapie_datalocaanotherbs.boosting),0x05)
        self.assertEqual(int(handler.dlmapie_datalocaanotherbs.preamble_index),0x4D)
        self.assertEqual(int(handler.dlmapie_datalocaanotherbs.no_ofdm_symbols),0xFA)
        self.assertEqual(int(handler.dlmapie_datalocaanotherbs.no_subchannels),0x2D)
        self.assertEqual(int(handler.dlmapie_datalocaanotherbs.repet_coding_ind),0x02)
        self.assertEqual(int(handler.dlmapie_datalocaanotherbs.cid),0xABAB)
        self.assertEqual(len(handler.burstprofile_list),3)                        
        
    
    def testDLMAPIEPhysicalModifierTimeShift(self):
        test_vector =   '\x00\x40\x23\xFF\xFF\x41\x02\xED\xAC\xBC\xA0\xA2\xDE\xAD\xDE\xAD'\
                        '\x00\x00\x03\x20\xD0\xC8\xB3\x7F\x81\xD7\x30\xC0\x80\xA3\x20\x42'\
                        '\x5F\x46\xCB'
        handlercollection = ManagementMessageHandler()
        handler = DLMAPDecoderTester.DLMapDecoderHandler()
        handlercollection.dlmaphandler  = handler
        PDUDecoder(handlercollection).Decode(test_vector)
        self.assertTrue(handler.dlmapie_phymod != None)
        self.assertEqual(int(handler.dlmapie_phymod.preamble_modifier_type),1)
        self.assertEqual(int(handler.dlmapie_phymod.preamble_time_shift_index),0x0A)
        self.assertEqual(int(handler.dlmapie_phymod.pilot_pattern_modifier),0x01)
        self.assertEqual(int(handler.dlmapie_phymod.pilot_pattern_index),0x03)
        self.assertEqual(len(handler.burstprofile_list),2)                                

    def testDLMAPIEPhysicalModifierFreqShift(self):
        test_vector =   '\x00\x40\x23\xFF\xFF\x41\x02\xED\xAC\xBC\xA0\xA2\xDE\xAD\xDE\xAD'\
                        '\x00\x00\x03\x20\xD0\xC8\xB3\x7F\x81\x52\x30\xC0\x80\xA3\x20\x90'\
                        '\x1C\x44\x80'
                        
        handlercollection = ManagementMessageHandler()
        handler = DLMAPDecoderTester.DLMapDecoderHandler()
        handlercollection.dlmaphandler  = handler
        PDUDecoder(handlercollection).Decode(test_vector)
        self.assertTrue(handler.dlmapie_phymod != None)
        self.assertEqual(int(handler.dlmapie_phymod.preamble_modifier_type),0)
        self.assertEqual(int(handler.dlmapie_phymod.preamble_frequency_shift_index),0x0A)
        self.assertEqual(int(handler.dlmapie_phymod.pilot_pattern_modifier),0)
        self.assertEqual(int(handler.dlmapie_phymod.pilot_pattern_index),2)        

        self.assertEqual(len(handler.burstprofile_list),2)    
    
    def testDLMAPIEBroadcastCtrlPtr(self):
        pass
    
        
        
class DCDDecoderTester(unittest.TestCase):
    class DCDDecoderHandler(DCDHandler):
        def __init__(self):
            super(DCDDecoderTester.DCDDecoderHandler,self).__init__()
            self.dcd = None
            self.dlburstprofiles = []
            self.channel_encodings = []
            
        def OnReceiveDCD(self,dcd):
            super(DCDDecoderTester.DCDDecoderHandler,self).OnReceiveDCD(dcd)
            self.dcd = dcd
    
        def OnReceiveBurstProfile(self,burstprofile):
            super(DCDDecoderTester.DCDDecoderHandler,self).OnReceiveBurstProfile(burstprofile)
            self.dlburstprofiles.append(burstprofile)
        
    
        def OnReceiveChannelEncoding(self,tlv):
            super(DCDDecoderTester.DCDDecoderHandler,self).OnReceiveChannelEncoding(tlv)
            self.channel_encodings.append(tlv)
            
            
    def testDCDBurstProfile(self):
        test_vector = '\x00\x00\x1C\xFF\xFF\x7C\x01\x00\x0A\x02\x02\xAA\xBB\x06\x01\xAB\x01'\
                      '\x04\x00\x96\x01\x02\x01\x04\x01\x96\x01\x03\x01\x04\x02\x96\x01'\
                      '\x04'
        handler = DCDDecoderTester.DCDDecoderHandler()
        mmhandler = ManagementMessageHandler()
        mmhandler.dcdhandler = handler
        PDUDecoder(mmhandler).Decode(test_vector)
        self.assertTrue(handler.dcd != None)
        self.assertEqual(len(handler.channel_encodings),2)
        self.assertEqual(len(handler.dlburstprofiles),3)        
    
    

def main_test():
    test_support.run_unittest(MACHeaderGenericDecoderTester,
                              TLVDecoderTester,
                              ToolsTester,
                              ULMapDecoderTester,
                              UCDDecoderTester,
                              DLMAPDecoderTester,
                              DCDDecoderTester)


        
if __name__ == '__main__':
    main_test()
